> {-# LANGUAGE ViewPatterns #-}

> import qualified Bs
> import qualified Ply
> import qualified OptBs
> import Iv (Iv)
> import System.Environment
> import System.Exit
> import System.Console.GetOpt
> import qualified Iv
> import qualified Numeric

> appID :: String
> appID = "sergei 0.2"

> data Flag = Verbose | Help deriving (Eq,Show)

> options :: [OptDescr Flag]
> options = 
>   [ Option ['v']     ["verbose"] (NoArg Verbose) "verbose output"
>   , Option ['h','?'] ["help"]    (NoArg Help)    "display help" ]

TODO make this a command line option

> epsilon :: Double
> epsilon = 0.00005

| Runs the optimizer. Arguments: problem, verbosity level

> runOn :: String -> Int -> IO ()
> runOn s v = do
>   putStrLn appID
>   putStrLn "Parsing..."
>   let Ply.Named (x,ps) = read s
>   ps `seq` putStrLn "Converting..."
>   let bs = map Bs.ofPly ps
>   foldl (flip seq) (putStrLn "Bounding...") bs
>   (Iv.dest -> (a,b),_) <- OptBs.infIO bs (map snd x) epsilon v
>   putStrLn (   "The minimum lies in:\n["
>             ++ Numeric.showFFloat Nothing a ", "
>             ++ Numeric.showFFloat Nothing b "]\n")

> main :: IO ()
> main = do
>   a <- getArgs
>   pn <- getProgName
>   let forHelp = "Type `" ++ pn ++ " -h' for help."
>   let help = usageInfo (appID ++ "\nUsage: " ++ pn ++ " [options] [file]") options
>   let (flags,file,errs) = getOpt Permute options a
>   if elem Help flags then putStr help >> exitSuccess
>     else if errs /= [] then error (concat errs ++ forHelp)
>     else do c <- case file of
>                    [] -> getContents
>                    [f] -> readFile f
>                    _ -> error ("More than one file given.\n" ++ forHelp)
>             runOn c (length $ filter (==Verbose) flags)
